//更改url中的参数值
function setURLArg(url,arg,arg_val){ 
    var pattern=arg+'-([^/]*)'; 
    var pattern2=arg+'=([^&]*)'; 
    var replaceText = '';
    if(arg_val!=''){
    	replaceText=arg+'-'+arg_val; 
    }
    if(url.match(pattern)){
    	if(arg_val==''){
    		var tmp2='/('+ arg+'-)([^/]*)\\//gi'; 
    		url=url.replace(eval(tmp2),''); 
    	}
        var tmp='/('+ arg+'-)([^/]*)/gi'; 
        tmp=url.replace(eval(tmp),replaceText); 
        return tmp; 
    }else if(url.match(pattern2)){
    	if(arg_val==''){
    		var tmp2='/\\&('+ arg+'=)([^&]*)/gi'; 
    		url=url.replace(eval(tmp2),''); 
    	}
        var tmp='/('+ arg+'=)([^&]*)/gi'; 
        tmp=url.replace(eval(tmp),replaceText.replace('-','=')); 
        return tmp; 
    }else if(arg_val!=''){ 
        if(url.match('[\?]')){ 
            return url+'&'+replaceText.replace('-','='); 
        }else{ 
            if(url.substr(-1)=='/'){
            	return url+replaceText+'/'; 
            }else{
            	return url+'/'+replaceText+'/';
            }
        } 
    } 
    return url; 
} 


//count box
$(function(){
	
	//数字放大动画
	$('.buycount .ctrl').each(function(n){
		$(this).click(function(){
			if(!($(this).hasClass('disabled'))){
				$(this).parent().find('input').addClass('tobig');
				var that  = $(this);
				setTimeout(function(){
					that.parent().find('input').removeClass('tobig');
				},501)
			}
			
		}) 
	})

    $(".buycount-ctrl").each(function(index,el){
        var $con = $(el);

        var $myInput = $con.find('input');
        var max_num = parseFloat($myInput.parent().attr('max_num'));
        if(max_num==0)
        	max_num = 1;
        $con.find('.minus').on('click',function(e){
            var val = $myInput.val();
            if(val <=1){
                $myInput.val(1);
            }else{
                $myInput.val(--val);
            }
            e.preventDefault();
            e.stopPropagation();


            render();
        });
        $con.find('.plus').on('click',function(e){
            var val = $myInput.val();
            if(val < 1){
                $myInput.val(1);
            }else{
                $myInput.val(parseFloat(val)+1>max_num ? max_num : ++val);
            }
            e.preventDefault();
            e.stopPropagation();

            render();
        });

        $myInput.on('blur',function(){
        	var num = parseFloat($myInput.val()).toFixed(0);
            if(num <= 1 || num=='NaN'){
                $myInput.val(1);
            }else{
            	var num_old = $myInput.parent().attr('current_num') ? $myInput.parent().attr('current_num') : $myInput.parent().attr('max_num');
            	$myInput.val(num>max_num ? num_old : num);
            }
            render();

        });

        function render(){
            var money_total = 0;
            $(".buycount-ctrl").each(function(){
            		money_total+=parseFloat($(this).find('input').val());
            })
            $('.buycount_total').text(money_total.toFixed(2));

            if($myInput.val() <= 1){
                $con.find('.minus').addClass('disabled');
            }else if($myInput.val()>=max_num){
                $con.find('.plus').addClass('disabled');
            }else{
                $con.find('.minus').removeClass('disabled');
                $con.find('.plus').removeClass('disabled');
            }
        }
        render();

    });

});



//iframe
function showIframe(url){
	var w=arguments[1]?arguments[1]:'90%';
	var h=arguments[2]?arguments[2]:'379px';
	$.colorbox({
		iframe:true,
		href:url,
		title:'立即购买',
		opacity:'0.3',
		overlayClose:false,
		width:w,
		height:h,
		maxHeight:'95%',
		maxWidth:'700px',
		fixed:true
	});
}



//上传图片预览  
//上传的时候可以生成固定宽高范围内的等比例缩放图  

//参数设置：  
//width                     存放图片固定大小容器的宽  
//height                    存放图片固定大小容器的高  
//imgDiv                    页面DIV的JQuery的id  
//maxSize                   图片大小最大限制(K)  
//imgType                   数组后缀名  
//**********************图片上传预览插件*************************  
(function($) {  
jQuery.fn.extend({  
  uploadPreview: function(opts) {  
      opts = jQuery.extend({  
          width: 0,  
          height: 0,  
          imgDiv: "#imgDiv",  
          maxSize:4,  
          imgType: ["gif", "jpeg", "jpg", "bmp", "png"],  
          callback: function() { return false; }  
      }, opts || {});  
      //var _self = this;  
      var _this = $(this);  
      var imgDiv = $(opts.imgDiv);  
      imgDiv.width(opts.width);  
      imgDiv.height(opts.height);  
         
      var version = parseInt($.browser.version,10);  
        
      autoScaling = function() {  
            
          if (version == 7 || version == 8  || version == 9) imgDiv.get(0).filters.item("DXImageTransform.Microsoft.AlphaImageLoader").sizingMethod = "image";  
          var img_width = imgDiv.width();  
          var img_height = imgDiv.height();  
          if (img_width > 0 && img_height > 0) {  
              /*
          	var rate = (opts.width / img_width < opts.height / img_height) ? opts.width / img_width : opts.height / img_height;  
              if (rate <= 1) {  
                  if (version == 7 || version == 8  || version == 9) imgDiv.get(0).filters.item("DXImageTransform.Microsoft.AlphaImageLoader").sizingMethod = "scale";  
                  imgDiv.width(img_width * rate);  
                  imgDiv.height(img_height * rate);  
              } else {  
                  imgDiv.width(img_width);  
                  imgDiv.height(img_height);  
              }
              */
          	imgDiv.width(opts.width);  
              imgDiv.height(opts.height);
              var left = 0;  
              var top = 0;  
              imgDiv.css({ "margin-left": left, "margin-top": top });  
              imgDiv.show();  
          }  
      },  
        
      createImg = function(){  
          imgDiv.html('');  
            
          var img = $("<img style='width:"+opts.width+"px;height:"+opts.height+"px;'/>");  
          imgDiv.replaceWith(img);  
          imgDiv = img;  
      },  

      _this.change(function() {  
            
          if (this.value) {  
          	var fileExt = this.value.substring(this.value.lastIndexOf('.') + 1);
          	if(fileExt==this.value)
          		fileExt = '';
              if (!RegExp("\.(" + opts.imgType.join("|") + ")$", "i").test(this.value.toLowerCase()) && fileExt!='') {  
                  alert("图片类型必须是" + opts.imgType.join("，") + "中的一种");  
                  this.value = "";  
                  return false;  
              }  
              imgDiv.hide();  
              if ($.browser.msie && version < 10) {  
                    
                  if (version == 6) {  
                        
                      var image = new Image();  
                      image.onload = function(){  
                          if( (image.fileSize/1024/1024) > opts.maxSize) {  
                              alert('图片大小不能超过'+opts.maxSize+'M');  
                              return false;  
                          }  
                      }  
                      image.src = 'file:///' + this.value;  

                      createImg();  
                      imgDiv.attr('src', image.src);  
                      autoScaling();  
                  }  else {  
                        
                      this.select();  
                      var img = document.selection.createRange().text;  
                      var image = new Image();  
                      image.onload = function(){  
                          if( (image.fileSize/1024/1024) > opts.maxSize) {  
                              alert('图片大小不能超过'+opts.maxSize+'M');  
                              return false;  
                          }  
                      }  
                      image.src = img;                              
                        
                      imgDiv.html('');                              
                      imgDiv.css({ filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image)" });  
                      imgDiv.get(0).filters.item("DXImageTransform.Microsoft.AlphaImageLoader").sizingMethod = "image";                             
                        
                      try {  
                          imgDiv.get(0).filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = img;  
                      } catch (e) {  
                          alert("无效的图片文件！");  
                          return;  
                      }                              
                        
                      setTimeout("autoScaling()", 100);                              
                  }  
              }  
              else {  
                  try{     
                      var file = null;  
                      var size = 0;  
                      if(this.files && this.files[0] ){  
                          file = this.files[0];   
                          size = file.size;  
                      }else if(this.files && this.files.item(0)) {                                  
                          file = this.files.item(0);     
                          size = file.fileSize;  
                      }   

                      if((size/1024/1024) > opts.maxSize){  
                          alert('图片大小不能超过'+opts.maxSize+'M');  
                          return false;  
                      }  
                        
                      createImg();  
                        
                      //Firefox 因安全性问题已无法直接通过input[file].value 获取完整的文件路径  
                      try{  
                          //Firefox7.0 以下                               
                          imgDiv.attr('src', file.getAsDataURL());  
                      }catch(e){  
                          //Firefox8.0以上                                
                          imgDiv.attr('src', window.URL.createObjectURL(file));  
                      }  
                        
                      imgDiv.css({ "vertical-align": "middle" });  
                      setTimeout("autoScaling()", 0);  
                  }catch(e){                            
                      //支持html5的浏览器,比如高版本的firefox、chrome、ie10  
                      if (this.files && this.files[0]) {                            
                          if((this.files[0].size/1024/1024) > opts.maxSize){  
                              alert('图片大小不能超过'+opts.maxSize+'M');  
                              return false;  
                          }  
                            
                          var reader = new FileReader();   
                          reader.onload = function (e) {                                        
                              imgDiv.attr('src', e.target.result);    
                          };  
                          reader.readAsDataURL(this.files[0]);   
                          setTimeout("autoScaling()", 100);  
                      }    
                  };  
              }  
          }  
      });  
  }  
});  
})(jQuery);  

	
	
